package com.leo.manage_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.manage_system.entity.Admin;
import com.leo.manage_system.entity.Res;
import com.leo.manage_system.mapper.AdminMapper;
import com.leo.manage_system.service.AdminService;
import com.leo.manage_system.utils.BeanMapTool;
import com.leo.manage_system.utils.JwtUtils;
import com.leo.manage_system.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author : ywzjks
 * @description : 描述
 * @Date : 2021/10/23 14:04
 */
/**
 * @author : ywzjks
 * @description : 描述
 * @Date : 2021/10/23 14:04
 */
@Api(tags = "管理员")
@RestController
@RequestMapping("/service_manage/admin")
@CrossOrigin
public class AdminController {
    final AdminService adminService;
    JwtUtils jwtUtils = new JwtUtils();
    @Autowired
    public AdminController(AdminService adminService){
        this.adminService=adminService;
    }



    @ApiOperation(value = "添加管理员")
    @PostMapping("addAdmin")
    public R addAdmin(@ApiParam(name = "管理员") @RequestBody Admin admin)
    {
//        System.out.println(admin);
        boolean code = adminService.addAdmin(admin);
        if(!code) {
            return R.error().message("添加失败");
        }
        return R.ok().message("添加成功");
    }

    @ApiOperation(value = "根据ID查询管理员")
    @GetMapping("getAdminById")
    public R getAdminById(
            @ApiParam(name = "id", value = "管理员ID", required = true)
            @RequestParam String id) {
        Admin admin = adminService.getById(id);
        return R.ok().data("item", admin);
    }


    @ApiOperation(value = "根据ID删除管理员")
    @DeleteMapping("deleteAdmin")
    public R deleteAdmin(
            @ApiParam(name = "id", value = "管理员ID", required = true)
            @RequestParam String id){
        boolean code = adminService.deleteAdmin(id);
        if(!code) {
            return R.error().message("删除失败");
        }
        return R.ok().message("删除成功");
    }



    @ApiOperation("管理员登录")
    @PostMapping("adminLogin")
    public R adminLogin(@ApiParam(name = "token", value = "token")
                        @RequestParam(value="token",defaultValue="") String token,
                        @ApiParam(name = "admin",value = "管理员密码",required = true)
                        @RequestBody Admin admin) {
        System.out.println(admin);
        if(!Objects.equals(token, "") && jwtUtils.verify(token)==1){
            return R.ok();
        }
        boolean code = adminService.adminLogin(admin);
        if(!code) {
            return R.error().message("帐号密码错误");
        }
        String username = admin.getUsername();
        String id = adminService.getAdminIdByUsername(username);
        String newToken = jwtUtils.createToken(id,username);
        System.out.println(newToken);
        return R.ok().message("登录成功").data("token",newToken);
    }


    @DeleteMapping("/deleteAdmin1")
    @ApiOperation("用户注销")
    public R deleteUser(@ApiParam(name = "token", value = "token", required = true)
                            @RequestParam(value="token") String token) {
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            return R.ok();
        }
    }
    @GetMapping("/getAdminInfo1")
    @ApiOperation("获取用户信息")
    public R getUserInfo(@ApiParam(name = "token", value = "token", required = true)
                         @RequestParam(value="token") String token) {
//        Admin admin = adminService.getUserWechatBySkey(skey);

        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            return R.ok();
        }
    }
//    @ApiOperation("获取用户信息")
//    @GetMapping("/getInfo/{id}")
//    public R info(@PathVariable String id) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Admin byUserName = adminService.getById(id);
//        if (byUserName == null) {
//            User byId = userService.getById(id);
//            if (byId != null) {
//
//            }
//            return R.error().message("未找到该用户");
//        }
//
//        return R.ok().data("name", byUserName.getUsername())
//                .data("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif")
//                .data("last_login", sdf.format(byUserName.getLastLogin()));
//    }
//    @ApiOperation(value = "修改管理员密码")
//    @PutMapping("updateAdminPassword")
//    public R updateAdminPassword(@ApiParam(name = "username",value = "管理员username",required = true)
//                                 @RequestParam String username,
//                                 @ApiParam(name = "oldPassword", value = "旧密码", required = true)
//                                 @RequestParam String oldPassword,
//                                 @ApiParam(name = "newPassword", value = "新密码", required = true)
//                                 @RequestParam String newPassword) {
//        boolean code = adminService.adminPasswordUpdate(username,oldPassword,newPassword);
//        if(!code) {
//            return R.error().message("密码错误修改失败");
//        }
//        return R.ok().message("修改成功");
//    }
//    @GetMapping("/getAdminInfo/{adminId}")

}



